package com.rc.saas.tenant.controller.enterprise;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.enterprise.EnterpriseMember;
import com.rc.saas.tenant.model.enterprise.EnterpriseSubmember;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.enterprise.EnterpriseSubMemberSearch;
import com.rc.saas.tenant.service.enterprise.EnterpriseSubmemberService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.vo.enterprise.EnterPriseSubMemberResult;
import com.rc.saas.tenant.vo.enterprise.EnterpriseSubmemberVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author frog
 * @date 2021/2/6
 */
@Controller
@RequestMapping("/enterpriseSubmember")
public class EnterpriseSubmemberController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(EnterpriseMemberController.class);


    @Autowired
    private EnterpriseSubmemberService enterpriseSubmemberService;

    /**
     * 跳转企业子会员页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String enterpriseSubmemberPage() {
        return "/templates/enterprise/enterpriseSubmember/enterpriseSubmember";
    }


    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(Model model, @PathVariable("id") Long id) {
        EnterpriseSubmember submember = enterpriseSubmemberService.selectByPrimaryKey(id);
        model.addAttribute("submember", submember);
        return "/templates/enterprise/enterpriseSubmember/enterpriseSubmember";
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable("id") Long id) {
        EnterpriseSubmember submember = enterpriseSubmemberService.selectByPrimaryKey(id);
        model.addAttribute("submember", submember);
        return "/templates/enterprise/enterpriseSubmember/enterpriseSubmember";
    }

    /**
     * 返回企业子会员列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listEnterpriseSubmember", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listEnterpriseSubmember(EnterpriseSubMemberSearch enterpriseSubMemberSearch) {
        TenantUser user = ShiroUtils.getSessionUser();
        enterpriseSubMemberSearch.setBelongEnterpriseId(user.getEnterpriseId());
        PageInfo<EnterPriseSubMemberResult> pageInfo = enterpriseSubmemberService.searchByCondition(enterpriseSubMemberSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转增加企业子会员页面(企业账户登录)
     *
     * @return
     */
    @RequestMapping(value = "/addEnterpriseSubmember", method = RequestMethod.GET)
    public String addEnterpriseSubmember(Model model) {
        TenantUser sessionUser = ShiroUtils.getSessionUser();
        model.addAttribute("enterpriseId", sessionUser.getEnterpriseId());
        return "/templates/enterprise/enterpriseMember/addEnterpriseSubmember";
    }


    /**
     * 跳转企业会员订单页面(企业账户登录)
     *
     * @return
     */
    @RequestMapping(value = "/enterpriseOrder", method = RequestMethod.GET)
    public String enterpriseOrder(Model model) {
        TenantUser sessionUser = ShiroUtils.getSessionUser();
        model.addAttribute("enterpriseId", sessionUser.getEnterpriseId());
        return "/templates/enterprise/enterpriseMember/enterpriseOrder";
    }

    /**
     * 跳转企业会员注销页面(企业账户登录)
     *
     * @return
     */
    @RequestMapping(value = "/cancel", method = RequestMethod.GET)
    public String cancel(Model model) {
        TenantUser sessionUser = ShiroUtils.getSessionUser();
        model.addAttribute("enterpriseId", sessionUser.getEnterpriseId());
        return "/templates/enterprise/enterpriseMember/cancelSubMember";
    }

    /**
     * 跳转子会员余额记录页面(企业账户登录)
     *
     * @return
     */
    @RequestMapping(value = "/subMemberAmountRecord", method = RequestMethod.GET)
    public String subMemberAmountRecord(Model model) {
        TenantUser sessionUser = ShiroUtils.getSessionUser();
        model.addAttribute("memberId", sessionUser.getEnterpriseMemberId());
        return "/templates/enterprise/enterpriseMember/subMemberAmountRecord";
    }

    @ResponseBody
    @RequestMapping(value = "/updateSubmemberStatus", method = RequestMethod.POST)
    public TResponse updateSubmemberStatus(Long id, String status) {
        EnterpriseSubmember submember = enterpriseSubmemberService.selectByPrimaryKey(id);
        submember.setStatus(status);
        enterpriseSubmemberService.updateByPrimaryKey(submember);
        return TResponse.SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/updateSubmemberToChannel", method = RequestMethod.POST)
    public TResponse updateSubmemberToChannel(@RequestParam("idList") List<Long> idList) {
        enterpriseSubmemberService.updateSubmemberToChannel(idList);
        return TResponse.SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/updateSubmember", method = RequestMethod.POST)
    public TResponse updateSubmember(EnterpriseSubmember submember, Boolean createUser) {
        enterpriseSubmemberService.updateSubmember(submember, createUser);
        return TResponse.SUCCESS();
    }
}
